package com.handpay.nocardpay.baselibrary.smartrepayment;

import com.handpay.nocardpay.baselibrary.commbase.BaseProtocolKey;
import com.handpay.nocardpay.baselibrary.config.NoCardPayConfig;
import com.handpay.nocardpay.baselibrary.config.SecureManager;
import com.handpay.nocardpay.baselibrary.logutils.HPLog;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by sxshi on 2017-12-4.
 */

public class SmartRePayment {

    /**
     * 智能还款
     *
     * @param userName
     * @param phoneNo
     * @param certNo
     * @throws Exception
     */
    public static String getSmartPayUrl(String host,String userName, String phoneNo, String certNo, String chinnel, String merchantCode) {
        String des3Key = NoCardPayConfig.SMARTPAY_3DESKEY;
//        String host = NoCardPayConfig.SMARTPAY_URL;
        Map<String, String> params = new HashMap<>();
        String realName = null;// 需要URLEncoder.encode
        try {
            realName = java.net.URLEncoder.encode(userName, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String phone = SecureManager.getInstance().des(phoneNo, 1, des3Key);// 需要指定3des加密
        String idCode = SecureManager.getInstance().des(certNo, 1, des3Key);// 需要指定3des加密
        String openid = null;// 非必传
        String channelCode = chinnel;//
        String merAgentCode = "10001";//
        params.put(BaseProtocolKey.REAL_NAME, realName);// encode
        params.put(BaseProtocolKey.PHONE, phone);// 3DES加密
        params.put(BaseProtocolKey.ID_CODE, idCode);// 3DES加密
        params.put(BaseProtocolKey.CHANNEL_CODE, channelCode);
        params.put(BaseProtocolKey.MER_AGENT_CODE, merAgentCode);
        String sign = SignUtil.getSignVeryfy(params, des3Key);
        StringBuilder url = new StringBuilder(host);
        url.append("?" + BaseProtocolKey.REAL_NAME + "=" + realName);
        url.append("&" + BaseProtocolKey.PHONE + "=" + phone);
        url.append("&" + BaseProtocolKey.ID_CODE + "=" + idCode);
        url.append("&" + BaseProtocolKey.CHANNEL_CODE + "=" + channelCode);
        url.append("&" + BaseProtocolKey.MER_AGENT_CODE + "=" + merAgentCode);
        url.append("&" + BaseProtocolKey.OPENID + "=" + openid);
        url.append("&" + BaseProtocolKey.MERCHANTCODE + "=" + SecureManager.getInstance().des(merchantCode, 1, des3Key));
        url.append("&" + BaseProtocolKey.SIGN + "=" + sign);
        HPLog.d("merchantCode:",merchantCode);
        HPLog.d("url", url);
        return url.toString();
    }

}
